package com.jyw.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jyw.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/10
 */

public class TimePickerHAdapter extends RecyclerView.Adapter<TimePickerHAdapter.TimePickerHHolder> {
    private Map<String, Boolean> data;
    private ArrayList<String> keyData;
    private Context mCtx;
    private Calendar sCalendar;
    private String backgroundKeyStr;
    private String finalDate;
    private OnDateSelectedListner listner;


    public TimePickerHAdapter(Context ctx) {
        this.mCtx = ctx;

        initData();

        keyData = new ArrayList<>();
        for (Object o : data.keySet().toArray()) {
            keyData.add((String) o);
        }
        /*Collections.reverse(keyData);*/
    }


    private void initData() {
        //保证map有顺序
        data = new LinkedHashMap<>();
        sCalendar = Calendar.getInstance(Locale.CHINA);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat fomater = new SimpleDateFormat("MM月dd日");
        String str0 = "今天\n" + (finalDate = fomater.format(sCalendar.getTime()));

        sCalendar.add(Calendar.DAY_OF_MONTH, 1);
        String str1 = "明天\n" + fomater.format(sCalendar.getTime());

        sCalendar.add(Calendar.DAY_OF_MONTH, 1);
        String str2 = getWeekOfDate(new Date(), 2) + "\n" + fomater.format(sCalendar.getTime());

        sCalendar.add(Calendar.DAY_OF_MONTH, 1);
        String str3 = getWeekOfDate(new Date(), 3) + "\n" + fomater.format(sCalendar.getTime());

        data.put(str0, Boolean.TRUE);
        data.put(str1, Boolean.FALSE);
        data.put(str2, Boolean.FALSE);
        data.put(str3, Boolean.FALSE);
    }

    private String getWeekOfDate(Date dt, int yanchi) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[(w + yanchi) % 7];
    }

    @SuppressLint("InflateParams")
    @Override
    public TimePickerHHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TimePickerHHolder(LayoutInflater.from(mCtx).inflate(R.layout.item_time_picker_h, null));
    }

    public String getFinalDate() {
        return finalDate;
    }

    public interface OnDateSelectedListner {
        void onSelected(boolean isToday);
    }

    public void setOnDateSelectedListner(OnDateSelectedListner listner) {
        this.listner = listner;
    }

    @Override
    public void onBindViewHolder(TimePickerHHolder holder, int position) {
        final String str = keyData.get(position);
        holder.tv.setText(str);
        if (data.get(str)) {
            backgroundKeyStr = str;
            holder.tv.setBackgroundResource(R.drawable.bg_rectangle_brown4);
        } else {
            holder.tv.setBackgroundColor(mCtx.getResources().getColor(R.color.F9FDFC));
        }
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.put(backgroundKeyStr, Boolean.FALSE);
                data.put(str, Boolean.TRUE);
                finalDate = str.substring(str.indexOf("\n") + 1, str.length());
                notifyDataSetChanged();
                if (str.startsWith("今天")) {
                    listner.onSelected(true);
                } else {
                    listner.onSelected(false);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class TimePickerHHolder extends RecyclerView.ViewHolder {
        TextView tv;

        TimePickerHHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
